var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var app = getApp();
var sliderWidth = 80; // 需要设置slider的宽度，用于计算中间位置

Page({
	data: {
		dataLoaded: false,

		dateList: [],

		activeIndex: 0,
		sliderOffset: 0,
		sliderLeft: 0,
		sliderWidth,
	},

	onLoad(options) {
		var _self = this;

		ajax.P('/openapi/CTechnician/getBespeakTime', {
			technician_id: options.techId
		}, function(res) {
			var data = res.result;
			var dateList = Object.keys(data);

			_self.setData({
				dataLoaded: true,
				winHei: app.getWinHei(),
				tabWid: dateList.length * 100,
				dateList,
				originTimeObj: data,
			});
		});
	},

	tabClick: function(e) {
		var curIndex = e.currentTarget.id;

		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: curIndex,
		});
	},

	_selected(e) {
		var {
			dateList,
			originTimeObj
		} = this.data;
		var {
			dayIndex,
			timeIndex,
			useful
		} = e.currentTarget.dataset;

		if(useful == 0){
			return;
		}

		var timeList = originTimeObj[dateList[dayIndex]];
		var timeObj = timeList[timeIndex];

		timeObj['active'] = !timeObj['active'];
		timeList.forEach(function(obj) {
			if (obj.begin != timeObj.begin && obj.end != timeObj.end) {
				obj['active'] = false;
			}
		});

		if (timeObj['active']) {
			this._timeObj = Object.assign({
				date: dateList[dayIndex]
			}, timeObj);
		}else{
			this._timeObj = null;
		}

		this.setData({
			originTimeObj
		});
	},

	_ok(evt) {
		app.globalData.preTimeObj = this._timeObj;
		wx.navigateBack();
		common.submitForm(evt.detail.formId);
	}
});